package com.zzzyh.forumsystemclient.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 郑雨杭
 * @create 2025/5/27 - 15:38
 * 统一接口返回格式
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AppResult <T>{
    // 状态码
    private Integer code;
    // 提示信息
    private String message;
    // 数据
    private T data;
    // 额外的数据
    private Map<String,Object> extra = new HashMap<>();

    public AppResult(String message, Integer code) {
        this.message = message;
        this.code = code;
    }

    public static AppResult<String> failed(int code, String message) {
        return new AppResult<>(message, code);
    }


    // 设置额外数据字段
    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }

    // 向额外数据中放入键值对
    public AppResult<T> putExtra(String key, Object value) {
        this.extra.put(key, value);
        return this;
    }

    // 各种成功返回

    public static <T> AppResult<T> success() {
        AppResult<T> result = new AppResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    public static <T> AppResult<T> success(String message) {
        AppResult<T> result = new AppResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(message);
        return result;
    }

    public static <T> AppResult<T> success(T data) {
        AppResult<T> result = new AppResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> AppResult<T> success(String message, T data) {
        AppResult<T> result = new AppResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> AppResult<T> success(T data, String key, Object value) {
        AppResult<T> result = success(data);
        result.putExtra(key, value);
        return result;
    }

    public static <T> AppResult<T> success(String message, T data, String key, Object value) {
        AppResult<T> result = success(message, data);
        result.putExtra(key, value);
        return result;
    }

    public static <T> AppResult<T> success(ResultCode resultCode) {
        AppResult<T> result = new AppResult<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }
    public static <T> AppResult<T> success(ResultCode resultCode, T data) {
        AppResult<T> result = new AppResult<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        result.setData(data);
        return result;
    }

    // 各种失败返回

    public static <T> AppResult<T> failed() {
        AppResult<T> result = new AppResult<>();
        result.setCode(ResultCode.FAILED.getCode());
        result.setMessage(ResultCode.FAILED.getMessage());
        return result;
    }

    public static <T> AppResult<T> failed(String message) {
        AppResult<T> result = new AppResult<>();
        result.setCode(ResultCode.FAILED.getCode());
        result.setMessage(message);
        return result;
    }

    public static <T> AppResult<T> failed(String message, String key, Object value) {
        AppResult<T> result = failed(message);
        result.putExtra(key, value);
        return result;
    }

    public static <T> AppResult<T> failed(ResultCode resultCode) {
        AppResult<T> result = new AppResult<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }
}
